package com.yanqu.road.dao.impl.activity.firework;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.newyear2023.FireWorkUserData;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class FireWorkUserDataDaoImpl extends TempDataObjectDao<FireWorkUserData> {

    public static String getUserDataSql = "select * from t_u_activity_fire_work where activityId=? and userId=?";
    public FireWorkUserData getUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserDataSql);
            int i = 1;
            call.setInt(i++,activityId);
                call.setLong(i++,userId);
                rs = call.executeQuery();
                while (rs.next()) {
                    FireWorkUserData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserDataSql = "insert into t_u_activity_fire_work(" +
            "activityId,userId,integral) " +
            " values (?,?,?)";
    @Override
    protected boolean addData(FireWorkUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getIntegral()));
        if (getDb().executeNoneQuery(insertUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserDataSql = "update t_u_activity_fire_work set integral=?" +
            " where activityId=? and userId=?";
    @Override
    protected boolean updateData(FireWorkUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getIntegral()));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected FireWorkUserData parseData(ResultSet rs) throws SQLException {
        FireWorkUserData data = new FireWorkUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setIntegral(rs.getLong("integral"));
        return data;
    }

}
